package com.tcd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcd.helper.redis.RedisHelper;
import com.tcd.helper.redis.ZSetRedisHelper;
import com.tcd.mapper.RoleMapper;
import com.tcd.pojo.Role;
import com.tcd.service.RoleService;
import com.tcd.utils.AccountUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author space
 * @since 2021-05-21
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired(required = false)
    RoleMapper roleMapper;
    private static final String REDIS_VAL_NAME = "roleSet";
    private static final String REDIS_VAL_KEY = "roleId";

    @Override
    public List<Role> getRoleFilter(Integer pageIndex, Integer pageSize, String name, Integer permissionId) {
        if (!RedisHelper.hasKey(REDIS_VAL_NAME)) {
            List<Role> list = roleMapper.selectRoleFilter(1, 0, null, null);
            ZSetRedisHelper.addSet(REDIS_VAL_NAME, REDIS_VAL_KEY, list, true);
        }
        if (AccountUtils.isNull(name) && AccountUtils.isAllNull()) {
            return ZSetRedisHelper.getValSet(REDIS_VAL_NAME, Role.class, (pageIndex - 1) * pageSize, pageSize);
        } else {
            return roleMapper.selectRoleFilter(pageIndex, pageSize, name, permissionId);
        }
    }

    @Override
    public Integer getRoleFilterCount(String name, Integer permissionId) {
        if (AccountUtils.isNull(name) && AccountUtils.isAllNull()) {
            return ZSetRedisHelper.getValCountSet(REDIS_VAL_NAME);
        } else {
            return roleMapper.selectRoleFilterCount(name, permissionId);
        }
    }

    @Override
    public Integer addRole(Role role) {
        int row = roleMapper.insert(role); //mybatis-plus 添加
        if (row > 0) {
            ZSetRedisHelper.addSet(REDIS_VAL_NAME, REDIS_VAL_KEY, role);
        }
        return row;
    }

    @Override
    public Integer updateRole(Role role) {
        int row = roleMapper.updateById(role); //mybatis-plus 修改
        if (row > 0) {
            ZSetRedisHelper.updateSet(REDIS_VAL_NAME, REDIS_VAL_KEY, roleMapper.selectRoleById(role.getRoleId()));
        }
        return row;
    }

    @Override
    public Integer delRoles(Integer[] ids) {
        if (ids != null && ids.length > 0) {
            for (Integer id : ids) {
                roleMapper.deleteRolePermission(id);
            }
            int row = roleMapper.deleteBatchIds(Arrays.asList(ids));   //mybatis-plus 批量删除
            if (row > 0) {
                ZSetRedisHelper.deleteSet(REDIS_VAL_NAME, ids);
            }
            return row;
        }
        return 0;
    }

    @Override
    public Integer setRolePermission(Integer roleId, Integer[] permissionIds) {
        int row = roleMapper.deleteRolePermission(roleId);
        if (permissionIds.length > 0) {
            row += roleMapper.insertRolePermission(roleId, permissionIds);
        }
        if (row > 0) {
            ZSetRedisHelper.updateSet(REDIS_VAL_NAME, REDIS_VAL_KEY, roleMapper.selectRoleById(roleId));
        }
        return row;
    }
}
